<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;

class SchoolController extends AdminController {
	public function _initialize(){
        parent::_initialize();
    }
	
	 /**
     * 会员列表
     */
    public function index(){
		$id = I('id')?I('id'):0;
		$name = I('name');
		$d_id = I('d_id');
		if(!empty($d_id)){
			$delete['id'] = array('in',$d_id);
			$re = M('Type')->where($delete)->setField('status',-1);
			if(!$re){
				$this->error('删除失败');
			}
		}
		
		$post = array('id'=>$id,'name'=>$name);
		
		if(!empty($name)){
			$where['name'] = array('like',"%{$name}%");
		}
		
		$where['parent_id'] = $id;
		$where['status'] = 0;
		$count = M("Type")->where($where)->count();
		$Page  = new \Think\Page($count,20);
		setPageParameter($Page, $post);
		$show = $Page->show();
		$list = M('Type')->where($where)->order("add_time desc")->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$up_school = M('Type')->where("id={$id}")->find();
		$up_school['level'] = empty($up_school)?0: $up_school['level']+1;
		$this->assign('up_school',$up_school);
		$this->assign('post',$post);
		$this->assign('list',$list);
		$this->display();
    }
	
	public function del(){
		$id = I('id');
		if(!empty($id)){
			$result = M('Type')->where("id={$id}")->find();
			$re = M('Type')->where("id={$id}")->setField('status',-1);
			if($re){
				$this->success('删除成功',U('School/index',array('id'=>$result['parent_id'])));
			}else{
				$this->error('删除失败');
			}
		}else{
			$this->error('删除失败');
		}
	}
	
	public function add(){
		$id = I('id');
		if(!empty($id)){
			$list = M('Type')->where("id={$id}")->find();
			$this->assign('list',$list);
		}
		
		$this->display();
		
	}
	
	public function save(){
		$parent_id = I('parent_id');
		$level = I('level');
		$name = I('name');
		if(empty($name)){
			$this->error('请填写名称');
		}
		
		$data['add_time'] = time();
		$data['level'] = $level?$level:0;
		$data['name'] = $name;
		$data['parent_id'] = $parent_id;
		$data['status'] = 0;
		
		$re = M('Type')->add($data);
		if($re){
			$this->success('添加成功',U('School/index',array('id'=>$parent_id)));
		}else{
			$this->error('添加失败');
		}
		
	}
}
	
